#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

int main(int argc, char **argv) {
	int fd1, fd2;
	int cnt;
	char buf[256];

	if (argc < 3)
		return 0;

	fd1 = open(argv[1], O_RDONLY);

	//printf("%s\n", strerror(0));

	if (-1 == fd1) {
		printf("Error: %s\n", strerror(errno));
		return -1;
	}

	fd2 = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0666);

	if (-1 == fd2) {
		printf("Error: %s\n", strerror(errno));
		close(fd1);
		return -1;
	}

	printf("Cur Offset: %ld\n", lseek(fd1, 10, SEEK_CUR));
	read(fd1, buf, 10);
	printf("Cur Offset: %ld\n", lseek(fd1, 0, SEEK_CUR));

	while (1) {
		cnt = read(fd1, buf, sizeof (buf));

		if (0 == cnt) {
			break;
		} else if (-1 == cnt) {
			return -1;
		} else {
			write(fd2, buf, cnt);
		}
	}

	return 0;
}
